
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of hoidap
 *
 * @author Bui
 */
class Hoidap extends CI_Controller {

    private $title = 'Hỏi đáp | Gia sư 7s - học là giỏi';
    private $popup = 0;

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('hoidap_model');
        $this->load->model('hoidap_traloi_model');
    }

    function show_all_questions() {
        $this->load->helper("url");
        $this->load->library("pagination");
        $this->load->model("Hoidap_model");
        $this->load->library('uri');
        $this->load->library('pagination');
        $tuychinh['base_url'] = base_url() . "index.php/hoidap/hoidap/show_all_questions";
        $tuychinh["total_rows"] = count($this->Hoidap_model->load_all_questions());
        $tuychinh['per_page'] = 10;
        $tuychinh["uri_segment"] = 4;
        $tuychinh['full_tag_open'] = '<ul class="pagination">';
        $tuychinh['full_tag_close'] = '</ul>';
        $tuychinh['prev_link'] = '&laquo;';
        $tuychinh['prev_tag_open'] = '<li>';
        $tuychinh['prev_tag_close'] = '</li>';
        $tuychinh['next_link'] = '&raquo;';
        $tuychinh['next_tag_open'] = '<li>';
        $tuychinh['next_tag_close'] = '</li>';
        $tuychinh['cur_tag_open'] = '<li class="active"><a href="#">';
        $tuychinh['cur_tag_close'] = '</a></li>';
        $tuychinh['num_tag_open'] = '<li>';
        $tuychinh['num_tag_close'] = '</li>';

        $tuychinh["num_links"] = round($tuychinh["total_rows"] / $tuychinh["per_page"]);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

        $this->pagination->initialize($tuychinh);
        $tuychinh["records"] = $this->Hoidap_model->fetch($tuychinh["per_page"], $page);
        $tuychinh['link'] = $this->pagination->create_links();
        $tuychinh['title'] = $this->title;
        //$this->load->view('user/hoidap/list_hoidap',$tuychinh);



        if ($this->popup == 1) {
            $this->showUI_2('user/hoidap/list_hoidap', 'user/hoidap/creating_question', $tuychinh);
            $this->popup = 0;
        }
        else
            $this->showUI('user/hoidap/list_hoidap', $tuychinh);
    }

    function create_image_crud($session) {
        $this->load->database();
        $this->load->library('image_crud');
        $image_crud = new image_CRUD();
        $image_crud->set_table('gs7s_hoidap_hinhanh_tmp');
        //If your table have by default the "id" field name as a primary key this line is not required
        $image_crud->set_primary_key_field('id');

        $image_crud->set_url_field('url');
        $image_crud->set_image_path('public/image');
        $session_data = $this->session->userdata('logged_in');
        $image_crud->set_relation_field('id_thanhvien', $session_data['id']);
        $image_crud->set_session_work('session', $session);
        return $image_crud->render('hoidap/');
    }

    function create_image_crud_gallery($id_hoidap) {
        $this->load->database();
        $this->load->library('image_crud');
        $image_crud = new image_CRUD();

        $image_crud->unset_upload();
        $image_crud->unset_delete();

        $image_crud->set_primary_key_field('id');
        $image_crud->set_url_field('url');
        $image_crud->set_table('gs7s_hoidap_hinhanh')
                ->set_image_path('public/image')
                ->set_relation_field('id_hoidap', $id_hoidap);

        return $image_crud->render('hoidap/');
    }

    function showUI($path, $data) {
        $data['title'] = $this->title;
        $this->load->view('user/common/header_view', $data);
        $this->load->view('user/common/left_sidebar_view', $data);
        $this->load->view('user/common/head_content_view', $data);
        $this->load->view($path, $data);
        $this->load->view('user/common/right_sidebar_view', $data);
        $this->load->view('user/common/footer_view', $data);
    }

    function showUI_2($path1, $path2, $data) {
        $data['title'] = $this->title;
        $this->load->view('user/common/header_view', $data);
        $this->load->view('user/common/left_sidebar_view', $data);
        $this->load->view('user/common/head_content_view', $data);
        $this->load->view($path1, $data);
        $this->load->view($path2, $data);
        $this->load->view('user/common/right_sidebar_view', $data);
        $this->load->view('user/common/footer_view', $data);
    }

    function add_comment() {

        // You have to load url helper before you can use redirect 
        // function in codeigniter
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        // Load data of current user
        $session_data = $this->session->userdata('logged_in');

        //set message
        $this->form_validation->set_message('required', "%s không được để trống.");
        $this->form_validation->set_rules('comment_noidung', 'Trả lời', 'trim|required|xss_clean');
        if ($this->form_validation->run($this) == TRUE) {
            // Add data to variable '$data'
            $data['noidung'] = $this->input->post('comment_noidung');
            $data['hoidap_id'] = $this->input->post('hoidap_id');
            $data['thanhvien_id'] = $session_data['id'];

            // Call function create_comment in model 'hoidap_traloi_model'
            echo $this->hoidap_traloi_model->create_comment($data);

            // Run action show_id to see new comment (refresh)
            //        redirect('hoidap/hoidap/show_id', 'refresh');
        }
    }

    function add_question() {
        $this->load->helper('url');
        $this->load->library('session');
        $session_data = $this->session->userdata('logged_in');
        if ($session_data) {

            $this->load->helper('form');
            $this->load->library('form_validation');
            //set message
            $this->form_validation->set_message('required', "%s không được để trống.");
            $this->form_validation->set_message('max_length', "Số lượng ký tự của %s không vượt quá %s");

            $this->form_validation->set_rules('tieude', 'Tiêu đề', 'trim|required|max_length[150]|xss_clean');
            $this->form_validation->set_rules('noidung', 'Nội dung', 'trim|required|max_length[5000]|xss_clean|callback_submit_question');

            if ($this->form_validation->run($this) == FALSE) {
                $this->load->model('quanly_monhoc_model');
                $this->load->model('thanhvien_model');
                $session = null;
                $rsegment_array = $this->uri->rsegment_array();
                if (!isset($rsegment_array[3]) || !(isset($rsegment_array[3]) && ($rsegment_array[3] == 'upload_file' || (is_numeric($rsegments_array[3]) && $rsegments_array[4] == 'ajax_list') ))/* && $rsegment_array[3]!='ordering' && $rsegment_array[3]!='insert_title' */) {
                    $session = $this->thanhvien_model->load_signal_session();
                }
                //image_crud
                $data['image_crud'] = $this->create_image_crud($session);
                $data['session'] = $session;
                $data['monhoc'] = $this->quanly_monhoc_model->get_paged_list(NULL)->result_array();
                $this->showUI('user/hoidap/create_hoidap', $data);
            }
        } else {
            $this->session->set_userdata('popup', 1);
            redirect('login', 'refresh');
        }
    }

    function submit_question() {
        $this->load->library('session');
        $session_data = $this->session->userdata('logged_in');

        $data = array(
            'tieude' => $this->input->post('tieude'),
            'noidung' => $this->input->post('noidung'),
            'chuquan_id' => $session_data['id'],
            'monhoc_id' => $this->input->post('monhoc_id')
        );

        $this->load->model('hoidap_model');
        $this->load->model('hoidap_hinhanh_tmp_model');
        $this->load->model('hoidap_hinhanh_model');
        $this->load->model('thanhvien_model');
        $this->hoidap_model->create_question($data);
        //lay id hoidap
        $hoidap_new = $this->hoidap_model->id_hoidap_thanhvien($session_data['id'])->row();
        //copy anh tu tb tmp sang tb hoidap_hinhanh
        $session = $this->input->post('session');
        $this->hoidap_hinhanh_model->insert_image($hoidap_new->id, $session);
        //xoa anh ben tbl_tmp
        $this->hoidap_hinhanh_tmp_model->delete_image($session);

        $this->showUI('user/hoidap/hoidap_dangcauhoithanhcong', $data = '');
    }

    function show_id() {
        $this->load->model('quanly_thanhvien_model');
        $this->load->model('quanly_monhoc_model');

        $id = $this->input->post('id_hoidap');
        $data['question'] = $this->hoidap_model->load_question($id)->row_array();
        $data['comment'] = $this->hoidap_traloi_model->load_comment_by_question($id)->result_array();
        $thanhvien = $this->quanly_thanhvien_model->get_by_id($data['question']['chuquan_id'])->row_array();
        $monhoc = $this->quanly_monhoc_model->get_by_id($data['question']['monhoc_id'])->row_array();

        $data['username'] = $thanhvien['username'];
        $data['monhoc'] = $monhoc['TenMonHoc'];
        //image_crud
        $data['image_crud'] = $this->create_image_crud_gallery($id);

        $this->showUI('user/hoidap/hoidap_detail', $data);
    }

}

?>
